import java.util.Stack;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-05-15
 * Time：15:13
 */
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<String> stack = new Stack<>();
        for (String elem : tokens) {
            //如果为算符，就出栈并计算
            if (elem.equals("+") || elem.equals("-")
                    || elem.equals("*") || elem.equals("/")) {
                int x1 = Integer.parseInt(stack.pop());
                int x2 = Integer.parseInt(stack.pop());
                switch (elem) {
                    //将计算后的结果入栈
                    case "+":
                        stack.push(String.valueOf(x2 + x1));
                        break;
                    case "-":
                        ;
                        stack.push(String.valueOf(x2 - x1));
                        break;
                    case "*":
                        ;
                        stack.push(String.valueOf(x2 * x1));
                        break;
                    case "/":
                        ;
                        stack.push(String.valueOf(x2 / x1));
                        break;
                }
            } else {
                //否则就入栈
                stack.push(elem);
            }
        }
        return Integer.parseInt(stack.pop());
    }
}
public class Test2 {


    public static void main(String[] args) {
        Solution solution = new Solution();
        int ret = solution.evalRPN(new String[]{"4","13","5","/","+"});
        System.out.println(ret);
    }
}
